from django.core.context_processors import csrf
from django.shortcuts import render_to_response, get_object_or_404
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.template import Context, loader
from notebook.models import Note, NoteForm
from django.http import Http404

"""
def index(request):
    latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
    t = loader.get_template('polls/index.html')
    c = Context({
            'latest_poll_list' : latest_poll_list,
    })
    return HttpResponse(t.render(c))

def detail(request, poll_id):
    '''
    # Detailed implementation
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404
    '''
    p = get_object_or_404(Poll, pk=poll_id)
    # A shortcut
    # No need Context, loader and HttpResponse
    return render_to_response('polls/detail.html', {'poll' : p},
            context_instance=RequestContext(request))

def results(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    return render_to_response('polls/results.html', {'poll' : p})
"""

def graphics(request):
    laptop = "en_notebook_132x26.png"
    c = {'picture': laptop}
    return render_to_response('notebook/index.html', c, RequestContext(request))


def vote(request, poll_id):
    p = get_object_or_404(Poll, pk=poll_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except(KeyError, Choice.DoesNotExist):
        return render_to_response('polls/detail.html', 
            {'poll' : p, 'error_message' : "You didn't select a choice.", }, 
            context_instance=RequestContext(request))
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if
        # a user hits the Back button.
        return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))

def addnote(request):
    if request.method == 'POST':
        print request.POST
        form = NoteForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']

            form.save()
        else:
            print form.errors
    form = NoteForm(auto_id=True)

    return render_to_response('notebook/addnote.html', {
        'form' : form,
        'form_errors' : form.errors,
    }, context_instance=RequestContext(request))

def intro(request):
    if request.method == 'POST':
        print request.POST
        form = NoteForm(request.POST)
        if form.is_valid():
            title = form.cleaned_data['title']
            content = form.cleaned_data['content']

            form.save()
        else:
            print form.errors
    form = NoteForm(auto_id=True)

    return render_to_response('notebook/intro.html', {
        'form' : form,
        'form_errors' : form.errors,
    }, context_instance=RequestContext(request))

def year_archive(request, year):
    notes = get_object_or_404(Note, title=u'more')
    #notes = Note()

    return render_to_response('notebook/archive.html', {
        'notes' : notes,
    })
    pass

def month_archive(request, year, month):
    note = Note()
    return render_to_response('notebook/archive.html', {
        'form' : note,
    })
    pass

def note_detail_by_date(request, year, month, day):
    note = Note()
    return render_to_response('notebook/archive.html', {
        'form' : note,
    })
    pass

def note_detail_by_id(request, note_id):
    note = get_object_or_404(Note, pk=note_id)
    return render_to_response('notebook/note_details.html', {
        'note' : note,
    })

def note_detail_by_title(request, note_title):
    note = get_object_or_404(Note, title=note_title)
    return render_to_response('notebook/note_details.html', {
        'note' : note,
    })

def thanks(request):
    form = ContactForm()
    return render_to_response('notebook/thanks.html', {
        'form' : form,
    })

